package com.android.hct.uart_weight_demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import android_serialport_api.FixSizeLinkedList;

public class MainActivity extends Activity {
    TextView t_weight;
    TextView t_unit,tvLog;
    Button b_zero;
    SerialPortUtils serialPortUtils;
    private Handler handler=null;
    byte[] commbuf=new byte[4096];
    int pCommbuf=0;
    int length;
    Spinner spinner_port;
    Button button_open,button_peel;
    private SerialPortFinder mSerialPortFinder;
    FixSizeLinkedList logList=new FixSizeLinkedList(10);
    String portdevicepath[]=null;

    SCALENOW scalenow=new SCALENOW();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建属于主线程的handler

        handler=new Handler();
        t_weight=(TextView) findViewById(R.id.textView_weight);
        tvLog=(TextView) findViewById(R.id.textView_weight);
        t_unit=(TextView) findViewById(R.id.textView_unit);
        serialPortUtils=new SerialPortUtils();
        //设置读到数据后的操作
        serialPortUtils.setOnDataReceiveListener(new SerialPortUtils.OnDataReceiveListener() {
            @Override
            public void onDataReceive(byte[] buffer, int size) {
                for(int i = 0; i < size; i++) {
                    if (buffer[i] == 0x2){
                        pCommbuf = 0; //判断起始符
                        commbuf=new byte[4096];
                    }
                    else if ((buffer[i] == 0x0a)||(buffer[i] == 0x03)) {    //判断结束符
                        if(commbuf[pCommbuf-1]==0x0d)pCommbuf-=1;
                        commbuf[pCommbuf]=0;
                        length=pCommbuf;
                        pCommbuf=0;
                        //收到数据后的处理
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                addLog(commbuf);
                                if((commbuf[0]=='S')&&(commbuf[1]=='Z'))
                                {
                                    Toast.makeText(MainActivity.this,"归零成功",Toast.LENGTH_SHORT).show();
                                }
                                else {
                                    GetWeight(commbuf);
                                    t_weight.setText(scalenow.sformatNetWeight);
                                    t_unit.setText(scalenow.sUnit);
                                }
                            }
                        });

                    }
                    else commbuf[pCommbuf++]=buffer[i];
                }
            }
        });

        mSerialPortFinder = new SerialPortFinder();
        portdevicepath=mSerialPortFinder.getAllDevicesPath();
        spinner_port=(Spinner)findViewById(R.id.spinner_port);
        button_open=(Button)findViewById(R.id.button_open);
        button_peel=(Button)findViewById(R.id.button_peel);

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, portdevicepath);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_port.setAdapter(adapter);
        button_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(serialPortUtils.serialPortStatus)
                {
                    serialPortUtils.closeSerialPort();
                    button_open.setText("open");
                } else {
                    int selectedItemPosition = spinner_port.getSelectedItemPosition();
                    if (selectedItemPosition<0){
                        Toast.makeText(getApplicationContext(),"请选择端口",Toast.LENGTH_LONG).show();
                        return;
                    }
                    String s = portdevicepath[selectedItemPosition];
                    serialPortUtils.openSerialPort(s, 9600);
                    if(serialPortUtils.serialPortStatus)button_open.setText("close");
                }
            }
        });

        b_zero=(Button)findViewById(R.id.button_zero);
        //归零
        b_zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             serialPortUtils.sendSerialPort("SZ\r\n","UTF-8");
            }
        });
        //去皮
        button_peel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             serialPortUtils.sendSerialPort("ST\r\n","UTF-8");
            }
        });
    }

    //
    void addLog(byte[] commbuf){
//        logList.add(new String(commbuf));
        tvLog.setText(commbuf.toString());
    }

    void GetWeight(byte[] databuf)
    {
        int i,j,offset=6;
        boolean	StartFalg=false;
        scalenow.bZeroFlag=true;
        scalenow.bOverFlag=false;
        scalenow.bWeiStaFlag=false;
        switch(databuf[0])
        {
            case 'o':
            case 'O':
                scalenow.bOverFlag=true;
                break;
            case 'u':
            case 'U':
                scalenow.bWeiStaFlag=false;
                offset=6;	//6
                break;
            case 's':
            case 'S':
                scalenow.bWeiStaFlag=true;
                break;
        }
        if(databuf[5]=='-')offset=5;
        for(i=0;i<14;i++)
        {
            if(databuf[i+offset]=='\'')databuf[i+offset]='.';
            if(StartFalg)
            {
                if(((databuf[i+offset]>'9')||(databuf[i+offset]<'.'))&&(!((databuf[i+offset]==' ')&&(databuf[i+offset+1]<='9'))))
                {
                    break;
                }
            }
            else if((databuf[i+offset]>='0')&&(databuf[i+offset]<='9'))
            {
                StartFalg=true;
                if(databuf[i+offset]!='0')scalenow.bZeroFlag=false;
            }
        }
        scalenow.sformatNetWeight=new String(databuf,offset,i);


        for(j=0;j<6;j++)
        {
            if(databuf[i+j+offset]<0x20)
            {
                break;
            }
        }
        scalenow.sUnit=new String(databuf,i+offset,j);
    }

    public static class SCALENOW {
        public String		sformatNetWeight="0";
        public String		sUnit="0";
        public boolean bWeiStaFlag;
        public boolean bZeroFlag;
        public boolean bOverFlag;
    }

}
